package com.ovopark.organize.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ovopark.organize.server.mapper.FranchiseesApprovalTemplateFieldSelectMapper;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateFieldSelect;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateFieldSelectFill;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateFieldSelectFillService;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateFieldSelectService;
import com.ovopark.organize.server.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wxb
 * @Title: FranchiseesApprovelServiceImpl
 * @Description: 加盟商立项模板字段选择项
 * @date 2024/4/11 15:51
 **/
@Service
@Slf4j
public class FranchiseesApprovalTemplateFieldSelectServiceImpl extends ServiceImpl<FranchiseesApprovalTemplateFieldSelectMapper, FranchiseesApprovalTemplateFieldSelect> implements FranchiseesApprovalTemplateFieldSelectService {

    @Resource
    private FranchiseesApprovalTemplateFieldSelectFillService franchiseesApprovalTemplateFieldSelectFillService;

    /**
     * @description: 获取字段下的选项配置 单选 多选 下拉 填空
     * @param: [fieldId, groupId]
     * @return: java.util.List<com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateFieldSelect>
     * @author wxb
     * @date: 2024/5/30 21:32
     */
    @Override
    public List<FranchiseesApprovalTemplateFieldSelect> getSelectListByFieldId(Integer fieldId) {
        if (fieldId == null) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<FranchiseesApprovalTemplateFieldSelect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesApprovalTemplateFieldSelect::getFieldId, fieldId);
        queryWrapper.eq(FranchiseesApprovalTemplateFieldSelect::getIsDeleted, 0);
        queryWrapper.orderByAsc(FranchiseesApprovalTemplateFieldSelect::getSort);
        List<FranchiseesApprovalTemplateFieldSelect> selectList = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(selectList)) {
            selectList.forEach(select -> {
                // 获取填空项
                 List<FranchiseesApprovalTemplateFieldSelectFill> selectFillList = franchiseesApprovalTemplateFieldSelectFillService.getSelectFillListBySelectId(select.getId());
                 select.setResource(selectFillList);
            });
        }
        return selectList;
    }

    @Override
    public void batchSaveSectionList(List<FranchiseesApprovalTemplateFieldSelect> selectList) {
        if (CollectionUtil.isNotEmpty(selectList)) {
            selectList.forEach(this::saveOrUpdateById);
        }
    }

    @Override
    public FranchiseesApprovalTemplateFieldSelect saveOrUpdateById(FranchiseesApprovalTemplateFieldSelect select) {
        if (select != null) {
            if (select.getId() != null) {
                baseMapper.updateById(select);
            } else {
                baseMapper.insert(select);
            }
        }
        return select;
    }

}
